//
//  XMFollowListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMIntimacyViewController.h"
#import "XMUserListTableViewCell.h"
#import "XMUserDetailViewController.h"
#import "XMConversationViewController.h"

@interface XMIntimacyViewController ()
@property (nonatomic, strong) NSMutableArray <XMUserModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation XMIntimacyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self guardListData];
    [self setupRefresh];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.dataArray = [NSMutableArray array];
    self.page = 1;
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self guardListDataRefresh];
    }];
    
    XMRefreshBackFooter *footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self guardListData];
    }];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
}

- (void)guardListDataRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self guardListData];
}

- (void)guardListData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/index/intimate" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMUserModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"还没有亲密好友哦～";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"亲密度" forState:UIControlStateNormal];
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMUserListTableViewCell)];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kUserListCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMUserListTableViewCell class])];
    [cell setupCellWithUser:self.dataArray[indexPath.row] type:(XMUserListCellIntimate)];
    XMWeakSelf(self);
    cell.actBlock = ^(XMUserModel * _Nonnull model, XMUserListCellType type) {
        XMStrongSelf(self);
        XMConversationViewController *conversationVC = [[XMConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:model.user_id];
        conversationVC.conversationType = ConversationType_PRIVATE;
        conversationVC.targetId = model.user_id;
        conversationVC.titleStr = model.showName;
        conversationVC.unReadMessage = 0;
        conversationVC.enableNewComingMessageIcon = YES; //开启消息提醒
        conversationVC.enableUnreadMessageIcon = YES;
        [self.navigationController pushViewController:conversationVC animated:YES];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserModel *model = self.dataArray[indexPath.row];
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    vc.userId = model.user_id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
